/*
 * @# GzipUtil.java 2011-12-2 下午04:00:12
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.experiments.memcached;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author lunx
 */
public class GzipUtil {

	public static byte[] gzip(byte[] bytes) throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		GZIPOutputStream gos = new GZIPOutputStream(bos);
		gos.write(bytes);
		gos.finish();
		gos.close();
		return bos.toByteArray();
	}

	public static byte[] unGzip(byte[] bytes) throws IOException {
		byte[] b = null;
		ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
		GZIPInputStream gis = new GZIPInputStream(bis);
		byte[] buf = new byte[1024];
		int num = -1;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while ((num = gis.read(buf, 0, buf.length)) != -1) {
			baos.write(buf, 0, num);
		}
		b = baos.toByteArray();
		baos.flush();
		baos.close();
		gis.close();
		bis.close();
		return b;
	}

}
